package com.wc.AlgoOJ_train.train6_图论.最小生成树.最小生成树2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/15 13:32
 * @description http://43.138.190.70:8888/p/GR0302
 */
public class Main {
    /**
     * kruskal算法
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = 1000010;
    static int[][] edges = new int[M][3];
    static int[] p = new int[N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) p[i] = i;
        for (int i = 1; i <= m; i++) {
            for (int j = 0; j < 3; j++) {
                edges[i][j] = sc.nextInt();
            }
        }
        int res = 0;
        int cnt = 0;
        Arrays.sort(edges, 1, m + 1, Comparator.comparingInt(o -> o[2]));
        for (int i = 1; i <= m; i++) {
            int a = edges[i][0], b = edges[i][1], c = edges[i][2];
            a = find(a);
            b = find(b);
            if (a != b) {
                res += c;
                p[a] = b;
                cnt++;
            }
        }
        if (cnt < n - 1) out.println("impossible");
        else out.println(res);
        out.flush();
    }

    static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
